package com.ysx.tmall.user.controller;

import com.ysx.tmall.user.bean.UserBean;
import com.ysx.tmall.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
public class UserController {


    @Autowired
    private UserService userService;

    /**
     * http://localhost:8080/user/find?id=1 http
     * @param id
     * @return
     */
    @GetMapping("/user/find")
    @ResponseBody
    public Map<String,Object> findUserById(Integer id){

        Map<String,Object> map = new HashMap<>();

        try {
            UserBean userBean = userService.findUserById(id);
            map.put("message","success");
            map.put("code","200");
            map.put("data",userBean);
        } catch (Exception exception) {
            map.put("message","failure");
            map.put("code","500");
        }

        return map;

    }
    @GetMapping("/user/add")
    @ResponseBody
    public Map<String,String> addUser(@RequestBody UserBean user){

        Map<String,String> map = new HashMap<>();

        try {
            userService.addUser(user);
            map.put("message","success");
            map.put("code","200");
        } catch (Exception exception) {
            map.put("message","failure");
            map.put("code","500");
        }

        return map;

    }
}
